﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.Net;

namespace Kaixin.Components.Net
{
    /// <summary>
    /// 模拟request请求的辅助类
    /// 创建：2009-12-9 14:54:21
    /// 版本：1.0
    /// 作者：吴鸣震
    /// </summary>
    class WebHelper
    {
        private static Dictionary<string, HtmlWeb> _dic = new Dictionary<string, HtmlWeb>();
        private static Dictionary<string, CookieContainer> _cookie = new Dictionary<string, CookieContainer>();

        private string _email;
        public WebHelper(string email)
        {
            _email = email;
        }


        public HtmlWeb GetHtmlWeb()
        {
            if (!_dic.ContainsKey(_email))
            {
                HtmlWeb hw = new HtmlWeb();
                hw.PreRequest = new HtmlWeb.PreRequestHandler(PreRequest);
                _dic.Add(_email, hw);
            }
            return _dic[_email];
        }

        private bool PreRequest(HttpWebRequest request)
        {
            if (!_cookie.ContainsKey(_email))
            {
                CookieContainer cc = new CookieContainer();
                _cookie[_email] = cc;
            }
            request.CookieContainer = _cookie[_email];
            return true;
        }
    }
}
